package com.xintonghua.bussiness.ui.fragment.client;

import android.os.Bundle;
import android.view.View;

import com.alibaba.fastjson.JSONObject;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.xintonghua.base.utils.MyUtils;
import com.xintonghua.base.utils.RefreshUtils;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.adapter.GroupLogAdapter;
import com.xintonghua.bussiness.base.BaseActivity;
import com.xintonghua.bussiness.bean.CustomersListItemBean;
import com.xintonghua.bussiness.bean.GroupLogBean;
import com.xintonghua.bussiness.bean.GroupLogResponse;
import com.xintonghua.bussiness.event.LogRefreshEventBus;
import com.xintonghua.bussiness.ui.user.log.AddLogActivity;
import com.xintonghua.bussiness.util.JsonUtil;
import com.xintonghua.bussiness.util.MyState;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by liqi on 2018/3/30.
 * 回访客户列表
 */

public class CustomersListActivity extends BaseActivity implements XRecyclerView.LoadingListener {
    @BindView(R.id.lv_log)
    XRecyclerView lvLog;
    CustomersListAdapter adapter;
    private List<CustomersListItemBean> list = new ArrayList<>();
    private int page = 1;
//    List<> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_log);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        initUI();
    }

    @Override
    public void initUI() {
        super.initUI();
        onSimpleActionBar();
        setSimpleActionBar("回访客户", R.drawable.icon_add, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openActivity(CustomersActivity.class);
            }
        });
        RefreshUtils.initList(this, lvLog, MyUtils.dip2px(this, 4), R.color.grey_bg);
        adapter = new CustomersListAdapter(this, list);
        lvLog.setLoadingMoreEnabled(false);
        lvLog.setAdapter(adapter);
        lvLog.setLoadingListener(this);
        find();
    }

    /**
     * 加载数据
     */
    private void find() {
//        httpCent.growthLog(page, this, MyState.requestOne);

        httpCent.getRevisitList(this, MyState.requestOne);
    }

    @Override
    public void dataBack(Object data, int order) {
        super.dataBack(data, order);
        switch (order) {
            case MyState.requestOne://回访客户列表
                try {
                    if (page == 1) {
                        list.clear();
                    }

                    list = JsonUtil.getListByJsonString(data.toString(), CustomersListItemBean.class);
                    adapter.setList(list);
                    adapter.notifyDataSetChanged();
                    RefreshUtils.onRefresh(adapter, lvLog);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                break;
        }
    }

    @Override
    public void onRefresh() {
        page = 1;
        find();
    }

    @Override
    public void onLoadMore() {
//        if (RefreshUtils.hasNext(list, adapter, lvLog)) {
//            page++;
//            find();
//        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(LogRefreshEventBus eventBus) {
        page = 1;
        find();
    }
}